home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / FDSet.c < prev    next >
C/C++ Source or Header  |  1990-05-22  |  5KB  |  218 lines

  1. /* FDSet.c -- Class for manipulating fd_set objects used by select(2)
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     May, 1989
  21.  
  22. Modification History:
  23.  
  24. $Log:    FDSet.c,v $
  25.  * Revision 3.0  90/05/22  10:42:05  kgorlen
  26.  * Release for 1st edition.
  27.  * 
  28. */
  29.  
  30. #include "FDSet.h"
  31. #include "nihclIO.h"
  32. #include "nihclconfig.h"
  33. #include <osfcn.h>
  34.  
  35. #define    THIS    FDSet
  36. #define    BASE    Object
  37. #define    BASE_CLASSES BASE::desc()
  38. #define    MEMBER_CLASSES
  39. #define VIRTUAL_BASE_CLASSES Object::desc()
  40.  
  41. DEFINE_CLASS(FDSet,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/FDSet.c,v 3.0 90/05/22 10:42:05 kgorlen Rel $",NULL,NULL);
  42.  
  43. unsigned FDSet::dtablesz = getdtablesize();
  44.  
  45. FDSet FDSet::operator-(const FDSet& a) const
  46. {
  47.     FDSet result;
  48.     register const long* p = (long*)&fs;
  49.     register const long* q = (long*)&a.fs;
  50.     register long* dst = (long*)&result.fs;
  51.     unsigned long n = div_sizeof_long(sizeof(fd_set));
  52.     while (n--) *dst++ = *p++ & ~*q++;
  53.     return result;
  54. }
  55.  
  56. FDSet FDSet::operator&(const FDSet& a) const
  57. {
  58.     FDSet result;
  59.     register const long* p = (long*)&fs;
  60.     register const long* q = (long*)&a.fs;
  61.     register long* dst = (long*)&result.fs;
  62.     unsigned long n = div_sizeof_long(sizeof(fd_set));
  63.     while (n--) *dst++ = *p++ & *q++;
  64.     return result;
  65. }
  66.  
  67. FDSet FDSet::operator|(const FDSet& a) const
  68. {
  69.     FDSet result;
  70.     register const long* p = (long*)&fs;
  71.     register const long* q = (long*)&a.fs;
  72.     register long* dst = (long*)&result.fs;
  73.     unsigned long n = div_sizeof_long(sizeof(fd_set));
  74.     while (n--) *dst++ = *p++ | *q++;
  75.     return result;
  76. }
  77.  
  78. FDSet FDSet::operator^(const FDSet& a) const
  79. {
  80.     FDSet result;
  81.     register const long* p = (long*)&fs;
  82.     register const long* q = (long*)&a.fs;
  83.     register long* dst = (long*)&result.fs;
  84.     unsigned long n = div_sizeof_long(sizeof(fd_set));
  85.     while (n--) *dst++ = *p++ ^ *q++;
  86.     return result;
  87. }
  88.  
  89. void FDSet::operator-=(const FDSet& a)
  90. {
  91.     register const long* q = (long*)&a.fs;
  92.     register long* dst = (long*)&fs;
  93.     unsigned long n = div_sizeof_long(sizeof(fd_set));
  94.     while (n--) { *dst = *dst & ~*q++; dst++; }
  95. }
  96.  
  97. void FDSet::operator&=(const FDSet& a)
  98. {
  99.     register const long* q = (long*)&a.fs;
  100.     register long* dst = (long*)&fs;
  101.     unsigned long n = div_sizeof_long(sizeof(fd_set));
  102.     while (n--) { *dst = *dst & *q++; dst++; }
  103. }
  104.  
  105. void FDSet::operator|=(const FDSet& a)
  106. {
  107.     register const long* q = (long*)&a.fs;
  108.     register long* dst = (long*)&fs;
  109.     unsigned long n = div_sizeof_long(sizeof(fd_set));
  110.     while (n--) { *dst = *dst | *q++; dst++; }
  111. }
  112.  
  113. void FDSet::operator^=(const FDSet& a)
  114. {
  115.     register const long* q = (long*)&a.fs;
  116.     register long* dst = (long*)&fs;
  117.     unsigned long n = div_sizeof_long(sizeof(fd_set));
  118.     while (n--) { *dst = *dst ^ *q++; dst++; }
  119. }
  120.  
  121. bool FDSet::isEmpty() const
  122. {
  123.     register const long* p = (long*)&fs;
  124.     register unsigned long n = div_sizeof_long(sizeof(fd_set));
  125.     while (n--) if (*p++ != 0) return NO;
  126.     return YES;
  127. }
  128.  
  129. const Class* FDSet::species() const
  130. // Return a pointer to the descriptor of the species of this class
  131. {
  132.     return &classDesc;
  133. }
  134.  
  135. bool FDSet::isEqual(const Object& p) const
  136. // Test two objects for equality
  137. {
  138.     return p.isSpecies(classDesc) && *this==castdown(p);
  139. }
  140.  
  141. unsigned FDSet::hash() const
  142. {
  143.     register unsigned h = 0;
  144.     register const unsigned* p = (unsigned*)&fs;
  145.     unsigned n = div_sizeof_int(sizeof(fd_set));
  146.     while (n--) h ^= *p++;
  147.     return h;
  148. }
  149.  
  150. unsigned FDSet::capacity() const
  151. {
  152.     return dtablesz;
  153. }
  154.  
  155. unsigned FDSet::size() const
  156. {
  157.     register unsigned k = 0;
  158.     register const unsigned char* p = (unsigned char*)&fs;
  159.     unsigned n = sizeof(fs);
  160.     while (n--) k += bitCount(*p++);
  161.     return k;
  162. }
  163.  
  164. int FDSet::compare(const Object&) const
  165. {
  166.     shouldNotImplement("compare");
  167.     return 0;
  168. }
  169.  
  170. void FDSet::deepenShallowCopy()
  171. {
  172. }
  173.  
  174. void FDSet::printOn(ostream& strm) const
  175. {
  176.     unsigned n = dtablesz;
  177.     for (int i = 0, j = 0; i < n; i++) {
  178.         if (isSet(i)) {
  179.             if (j++) strm << ',';
  180.             strm << i;
  181.         }
  182.     }
  183. }
  184.  
  185. FDSet::FDSet(OIOin& strm)
  186.     : BASE(strm)
  187. {
  188.     zero();
  189.     int n;
  190.     strm >> n;
  191.     while (n--) {
  192.         int i;
  193.         strm >> i;
  194.         set(i);
  195.     }
  196. }
  197.  
  198. void FDSet::storer(OIOout& strm) const
  199. {
  200.     BASE::storer(strm);
  201.     strm << size();
  202.     for (int i = 0; i<dtablesz; i++) {
  203.         if (isSet(i)) strm << i;
  204.     }
  205. }
  206.  
  207. FDSet::FDSet(OIOifd& fd)
  208.     : BASE(fd)
  209. {
  210.     fd.get((char*)&fs,sizeof(fs));
  211. }
  212.  
  213. void FDSet::storer(OIOofd& fd) const
  214. {
  215.     BASE::storer(fd);
  216.     fd.put((const char*)&fs,(unsigned)sizeof(fs));
  217. }
  218.